# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=file
pkgver=5.24
pkgrel=1
pkgdesc="File type identification utility"
arch=('i686' 'x86_64')
license=('custom')
groups=('base' 'base-devel')
url="http://www.darwinsys.com/file/"
depends=('gcc-libs' 'msys2-runtime' 'zlib')
makedepends=('python2' 'python3' 'zlib-devel')
options=('!libtool')
source=(ftp://ftp.astron.com/pub/${pkgname}/${pkgname}-${pkgver}.tar.gz
        file-5.17-msys2.patch)
md5sums=('ec161b5a0d2aef147fb046e5630b1408'
         'c767c9156ea98f38eab968b9d2c0198a')

prepare() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  patch -p1 -i ${srcdir}/file-5.17-msys2.patch

  autoreconf -fi
}

build() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  ./configure \
    --prefix=/usr \
    --build=${CHOST} \
    --host=${CHOST} \
    --target=${CHOST} \
    --enable-fsect-man5

  make
}

package() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  make DESTDIR=${pkgdir} install

  install -Dm644 COPYING ${pkgdir}/usr/share/licenses/${pkgname}/COPYING

  PYTHON_SITELIB=$(python2 -c 'from distutils.sysconfig import * ; print get_python_lib(0,0);')
  PYTHON3_SITELIB=$(python3 -c 'from distutils.sysconfig import * ; print(get_python_lib(0,0));')

  mkdir -p ${pkgdir}/$PYTHON_SITELIB
  mkdir -p ${pkgdir}/$PYTHON3_SITELIB
  cp -f ${srcdir}/${pkgname}-${pkgver}/python/magic.py ${pkgdir}/$PYTHON_SITELIB
  cp -f ${srcdir}/${pkgname}-${pkgver}/python/magic.py ${pkgdir}/$PYTHON3_SITELIB

  cat ${srcdir}/${pkgname}-${pkgver}/magic/Magdir/* > ${pkgdir}/usr/share/misc/magic
  cp -rf ${pkgdir}/usr/share/misc/magic ${pkgdir}/usr/share/magic
  mkdir -p ${pkgdir}/usr/share/file
  cp -rf ${pkgdir}/usr/share/magic ${pkgdir}/usr/share/file/magic
}
